package org.bleep.physics;

public class Motion
{
	public static float DEFAULT_MAX_VELOCITY = 9999.0f;
	public static float MIN_LIMIT = 0.01f;
	public static Motion STOP = new Motion(Vector2D.ZERO, Vector2D.ZERO);
	
	private Vector2D v, a;
	private float maxVelocity;
	
	public Motion()
	{
		v = new Vector2D();
		a = new Vector2D();
		maxVelocity = DEFAULT_MAX_VELOCITY;
	}
	
	public Motion(Vector2D v, Vector2D a)
	{
		this.v = v;
		this.a = a;
		maxVelocity = DEFAULT_MAX_VELOCITY;
	}
	
	public Motion(Vector2D v, Vector2D a, float max)
	{
		this.v = v;
		this.a = a;
		maxVelocity = max;
	}
	
	public void update()
	{
		if (a.getX() != 0 || a.getY() != 0)
		{
			Vector2D temp = Vector2D.add(v, a);
			if (temp.getMagnitude() < maxVelocity)
			{
				v = temp;
			}
			else
			{
				v = Vector2D.mult(Vector2D.normalize(temp), maxVelocity);
			}
		}
		if (Math.abs(v.getX()) < Motion.MIN_LIMIT)
		{
			v.setX(0.0f);
		}
		if (Math.abs(v.getY()) < Motion.MIN_LIMIT)
		{
			v.setY(0.0f);
		}
	}
	
	public void setMaxVelocity(float max)
	{
		maxVelocity = max;
	}
	
	public void setVelocity(Vector2D v)
	{
		this.v = v;
	}
	
	public void setAccel(Vector2D a)
	{
		this.a = a;
	}
	
	public void addVelocity(Vector2D v2)
	{
		v.add(v2);
	}
	
	public void addAccel(Vector2D a2)
	{
		a.add(a2);
	}
	
	public float getX()
	{
		return v.getX();
	}
	
	public float getY()
	{
		return v.getY();
	}
	
	public void stop()
	{
		v = Vector2D.ZERO;
		a = Vector2D.ZERO;
	}
	
	public String toString()
	{
		return "v: " + v + "\na: " + a;
	}
}